package com.evil.application.strategy.reporting;

import com.evil.application.enums.reporting.ReportingTypeEnum;
import com.evil.application.pojo.dto.reporting.ApplicationReportingDTO;
import com.evil.application.pojo.dto.reporting.data.ReportingDataRespDTO;
import com.evil.application.service.ElasticsearchService;
import com.evil.common.log.annotation.ExecuteTime;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.search.aggregations.Aggregations;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 报表类型-接口
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@Component
@AllArgsConstructor
public class ReportingDataChartStrategy {

    private final List<ReportingDataChart> chartList;

    private final ElasticsearchService elasticsearchService;

    public ReportingDataRespDTO calculation(ApplicationReportingDTO reportingDTO) {
        Aggregations aggregations = elasticsearchService.orderDataSummary(reportingDTO);
        return this.calculation(reportingDTO, aggregations);
    }

    @ExecuteTime(name = "应用报表，汇总时间")
    public ReportingDataRespDTO calculation(ApplicationReportingDTO reportingDTO, Aggregations aggregations) {
        for (ReportingDataChart reportingDataChart : chartList) {
            if (reportingDataChart.type().getId() == reportingDTO.getReportingType()) {
                return reportingDataChart.calculation(reportingDTO, aggregations);
            }
        }
        throw ReportingTypeEnum.getBusinessException();
    }
}
